package chapter04;

import java.math.BigInteger;

/**
 * ClassName: FindThefactorialSumAnswer
 * Package: chapter04
 * Description:
 *
 * @Author fjorid
 * @Create 2025/2/17 19:00
 * @Version 1.0
 */
public class FindThefactorialSumAnswer {



        public static void main(String[] args) {
            // 用于存储阶乘的和
            BigInteger sum = BigInteger.ZERO;
            // 循环计算 1 到 30 的阶乘
            for (int i = 1; i <= 30; i++) {
                // 调用 factorial 方法计算当前数字的阶乘
                BigInteger fact = factorial(i);
                // 将当前数字的阶乘累加到总和中
                sum = sum.add(fact);
            }
            System.out.println("1 到 30 的阶乘的和是: " + sum);
        }

        // 计算给定数字的阶乘
        public static BigInteger factorial(int n) {
            BigInteger result = BigInteger.ONE;
            for (int i = 1; i <= n; i++) {
                result = result.multiply(BigInteger.valueOf(i));
            }
            return result;
        }

}
